import pymysql


def regiter():
    print('用户注册')

    user = input('请输入用户名:')  # duiduo
    nickname = input('请输入昵称:')  # duiduo
    password = input('请输入密码:')  # 123

    # 连接数据
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        passwd='123456',
        charset='utf8',
        db='test01'
    )
    cursor = conn.cursor()

    # 执行sql语句
    sql = "INSERT INTO `role` (`username`, `password`, `nickname`, `user_face`, `create_time`, `update_time`) VALUES ('{}', '{}', '{}', NULL, now(), now())".format(user, password, nickname)
    cursor.execute(sql)
    conn.commit()

    # 关闭连接
    cursor.close()
    conn.close()


def login():
    print('用户登录')

    nickname = input('请输入昵称:')  # duiduo
    password = input('请输入密码:')  # 123

    # 连接数据
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        passwd='123456',
        charset='utf8',
        db='test01'
    )
    cursor = conn.cursor()

    # 执行sql语句
    sql = "SELECT * FROM role WHERE nickname = '{}' and password = '{}'".format(nickname, password)
    cursor.execute(sql)
    result = cursor.fetchone()

    # 关闭连接
    cursor.close()
    conn.close()

    if result:
        print('登录成功', result)
    else:
        print('登陆失败')


def run():
    choice = eval(input('1.注册 2.登录'))
    if choice == 1:
        regiter()
    elif choice == 2:
        login()
    else:
        print('输入错误')


if __name__ == '__main__':
    run()
